﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace Extensil.Common.CategoryHandler.Discovery
{
    public class DiscoverySection :ConfigurationElementCollection
    {
        public bool IsActive
        {
            get
            {
                return (Active.ToLower() == "yes" ||
                    Active.ToLower() == "y" ||
                    Active.ToLower() == "true" ||
                    Active.ToLower() == "on"
                    );
            }
            set
            {
                this.Active = value ? "yes" : "no";
            }
        }
        [ConfigurationProperty("active", IsRequired = false)]
        public string Active
        {
            get
            {
                return base["active"] as String;
            }
            set
            {
                base["active"] = value;
            }
        }
        public bool IsAutoUpdateEnabled
        {
            get
            {
                return (AutoUpdate.ToLower() == "yes" ||
                    AutoUpdate.ToLower() == "y" ||
                    AutoUpdate.ToLower() == "true" ||
                    AutoUpdate.ToLower() == "on"
                    );
            }
            set
            {
                this.AutoUpdate = value ? "yes" : "no";
            }
        }
        [ConfigurationProperty("autoUpdate", IsRequired = false)]
        public string AutoUpdate
        {
            get
            {
                return base["autoUpdate"] as String;
            }
            set
            {
                base["autoUpdate"] = value;
            }
        }
        [ConfigurationProperty("lastUpdate", IsRequired = false)]
        public DateTime LastUpdate
        {
            get
            {
                if(base["lastUpdate"] != null)
                {
                    DateTime dt = DateTime.MinValue;
                    DateTime.TryParse(base["lastUpdate"].ToString(), out dt);
                    return dt;
                }
                else
                    return DateTime.MinValue;
            }
            set
            {
                base["lastUpdate"] = value;
            }
        }
        [ConfigurationProperty("probeInterval", IsRequired = false)]
        public int ProbeInterval
        {
            get
            {
                return (String.IsNullOrEmpty(base["probeInterval"].ToString()) ? 14400000 : (int)base["probeInterval"]);
            }
            set
            {
                base["probeInterval"] = value;
            }
        }
        
        public Probe this[int index]
        {
            get
            {
                return base.BaseGet(index) as Probe;
            }
            set
            {
                if(base.Count > index && base.BaseGet(index) != null)
                {
                    base.BaseRemoveAt(index);
                }
                this.BaseAdd(index, value);
            }
        }
        public Probe this[string key]
        {
            get
            {
                return base.BaseGet(key) as Probe;
            }
        }

        public override bool IsReadOnly()
        {
            return false;
        }

        internal void Add(Probe prb)
        {
            this.BaseAdd(prb);
        }
        internal void Remove(Probe prb)
        {
            this.BaseRemove(prb.Name);
        }
        internal int IndexOf(Probe prb)
        {
            return base.BaseIndexOf(prb);
        }

        protected override ConfigurationElement CreateNewElement(string elementName)
        {
            return base.CreateNewElement(elementName);
        }
        protected override ConfigurationElement CreateNewElement()
        {
            return new Probe();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((Probe)element).Name;
        }

    }
}
